package com.link.base.base.system.service;

import com.link.base.base.system.dao.mybatis.mapper.PeriodMapper;
import com.link.base.base.system.model.Period;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.ServiceException;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.util.DateUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


/**
 * @author unknown
 * @date unknown
 */
@Service
public class PeriodServiceImpl extends BasicServiceImpl<Period> implements PeriodService {

    @Resource
    private PeriodMapper periodMapper;

    @Override
    public BasicMapper<Period> getBasicMapper() {
        return periodMapper;
    }

    @Override
    public List<Period> queryByDate(Period period) throws Exception {
        return periodMapper.queryByDate(period);
    }

    @Override
    public List<Period> getMonthPriod(Period period) throws Exception {
        period.setType("Month");
        String year = DateUtil.getCurrentYear();
        period.setPartYear(year);
        List<Period> periodList = periodMapper.queryAll(period);
        if (periodList == null || periodList.size() == 0) {
            throw new ServiceException("SYSTEM-003");
        }
        return periodList;
    }

    @Override
    public List<Period> queryMonthPeriodByDate(Period period) throws Exception {
        return periodMapper.queryMonthPeriodByDate(period);
    }

    /**
     * @Description:
     *提前处理唯一性冲突抛出的异常
     * @Author: liulu
     * @Date:14:49 2018/11/12
     * @param entity
     * @return:
     */

    @Override
    public void beforInsert(Period entity) throws Exception {
        Period period = new Period();
        period.setTitle(entity.getTitle());
        period.setPageFlag(false);
        period.setTotalFlag(false);
        List<Period> periodList = queryByExamplePage(period);
        if (!periodList.isEmpty()) {
            if (!(periodList.size() == 1 && periodList.get(0).getId().equals(entity.getId()))) {
                throw new ServiceException("SYSTEM-004");
            }

        }
    }
}
